﻿// ===============================================================================
// Project Name        :    Gfw.BLL.Basic.Facilities
// Project Description :    
// ===============================================================================
// Class Name          :    AwardAllot
// Namespace:          :    Gfw.BLL.Basic.Facilities
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/6/24 15:38:18
// Update Time         :    2012/6/24 15:38:18
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================

using System;
using Gfw.Infrastructure.Account;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Models.Account;
using Gfw.Repositories.Account;
using Gfw.Repository;

namespace Gfw.Infrastructure.Facilities
{
    public class AwardAllot : IFacility
    {
        /// <summary>
        /// 现金帐户分配比例
        /// </summary>
        protected const decimal CashRatio = 0.7M;

        protected string userId;
        protected decimal award;
        protected string taxationSerialNo;
        protected string awardSerialNo;
        protected UserTradeTypeEnum tradeType;
        protected decimal cash;
        protected decimal emoney;

        /// <summary>
        /// 将产生对用户帐户的更改
        /// </summary>
        /// <param name="userId">贡献用户id</param>
        /// <param name="award">得奖金额</param>
        /// <param name="awardSerialNo">动态奖金流水号</param>
        /// <param name="taxationSerialNo">综合税收编号</param>
        public AwardAllot(string userId, decimal award, string awardSerialNo, string taxationSerialNo,
                          UserTradeTypeEnum tradeType)
        {
            if (award <= 0)
                throw new GfwTransactionInvalidException(ConstMessage.AwardValueInvalid);
            this.userId = userId;
            this.award = award;
            this.taxationSerialNo = taxationSerialNo;
            this.awardSerialNo = awardSerialNo;
            this.tradeType = tradeType;

        }

        public decimal IndividualAward()
        {
            return award - cash - emoney;
        }


        public virtual void Execute(Repository.UnitOfWork unitOfWork = null)
        {
            if (unitOfWork == null)
                throw new GfwTransactionInvalidException(ConstMessage.TransactionInvalid);

            UnitOfWork work = unitOfWork;
           
            ActUserAccountinformationRepository userAccountRepository = new ActUserAccountinformationRepository();
         
            ActUserAccountinformation userAmount = userAccountRepository.Get(userId, work);
             //cash = Math.Floor(award*CashRatio);
             //emoney = Math.Floor(award*(1 - CashRatio));
            cash =Math.Round( award * CashRatio,4);
            emoney =award -cash;
            decimal companyDec = award - cash - emoney;
            userAmount.Cash += cash;
            userAmount.ElectronicMoney += emoney;

            userAccountRepository.Update(userAmount,work);

            if (cash > 0)
            {
                ActUserBargain4cashRepository cashRepository = new ActUserBargain4cashRepository();
                cashRepository.AddIncome(userId, (int)tradeType, cash, userAmount.Cash, awardSerialNo, work);
            }
            if (emoney > 0)
            {
                ActUserBargain4emoneyRepository emontyRepository = new ActUserBargain4emoneyRepository();
                emontyRepository.AddIncome(userId, (int)tradeType, emoney, userAmount.ElectronicMoney, 0,
                                           awardSerialNo, null, work);
            }
            if (companyDec > 0)
            {
                //ActCompanyBargainRepository companyRepository = new ActCompanyBargainRepository();
                //companyRepository.AddIncome(userId, (int)CompanyTradeTypeEnum.Decimal, companyDec, "",
                //                            awardSerialNo, null, work);
                throw new GfwTransactionInvalidException("已经取消零头转移了。");
            }

            
        }
    }
}
